package com.my.system.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.my.system.command.impl.EXITCommand;
import com.my.system.command.impl.HELPCommand;
import com.my.system.command.impl.LOGINCommand;
import com.my.system.command.impl.PRODUCTCommand;
import com.my.system.command.impl.SIGNUPCommand;
import com.my.system.object.client.Client;
import com.my.system.object.client.User;

public class SystemCommandFactory extends CommandFactory {
	private Client client;
	private User user;
	private static Map<CommandCode, Class<? extends Command>> map = new HashMap<>();
	static {
		map.put(CommandCode.EXIT, EXITCommand.class);
		map.put(CommandCode.HELP, HELPCommand.class);
		map.put(CommandCode.LOGIN, LOGINCommand.class);
		map.put(CommandCode.PRODUCT, PRODUCTCommand.class);
		map.put(CommandCode.SIGNUP, SIGNUPCommand.class);
	}

	public SystemCommandFactory(Client client, User user) {
		// TODO Auto-generated constructor stub
		this.client = client;
		this.user = user;
	}

	@Override
	public Command buildCommand(CommandCode code) {
		// TODO Auto-generated method stub
		Class<? extends Command> cla = map.get(code);
		try {
			Constructor<? extends Command> con = cla
					.getConstructor(new Class[] { Client.class, User.class });
			return con.newInstance(new Object[] { client, user });
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
